﻿using IService;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Models.MiniuiModes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace MiunUI.Controllers.Home
{  
    [Route("[controller]/[action]")]
    public class LoginController : Controller
    {
        public readonly IUserService _userService;
        public LoginController(IUserService userService
            )
        {
            _userService = userService;
        }
        [Route("/")]
        [AllowAnonymous]
        public IActionResult Login()
        {
            return View();
        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> CheckLogin(string username, string userpassword)
        {
            var Items = _userService.UserLogin(username, userpassword);
            if (Items == null)
            {
                return Ok(new BaseResult() { code = -1, msg = "登录失败，账号或密码错误" });
            }
            var claims = new List<Claim>();
            claims.Add(new Claim(ClaimTypes.Name, Items.USNAME));
            var claimsIdentity = new ClaimsIdentity(claims, "Cookies");
            await HttpContext.SignInAsync("Cookies", new ClaimsPrincipal(claimsIdentity));

            return Ok(new BaseResult() { code = 0, msg = "登录成功" });
        }
    }
}
